<?php

namespace App\Http\Controllers\VideoApi;

use App\Http\Config\ErrorCode;
use App\Library\UploadFile;
use App\Library\UpTools;
use App\Models\Admin\Picture;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UploadController extends BaseController
{
    public function uploadImage(Request $request)
    {
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        global $scf_data;
        if (!empty($scf_data)){  //配置云开发版
            $host = 'https://'.$scf_data['system']['bucketConfig']['bucket'].'.cos.'.$scf_data['system']['bucketConfig']['region'].'.myqcloud.com';
            $base64_img = trim($request->post('file',''));
            if (!$base64_img){
                $this->returnData = ErrorCode::$admin_enum['params_error'];
                $this->returnData['msg'] = '缺少文件';
                return $this->return_result($this->returnData);
            }
            $type = ['image/jpg', 'image/png', 'image/jpeg', 'image/bmp'];
            $files = json_decode($base64_img,true);
            if (!in_array($files['type'],$type)){
                $this->returnData = ErrorCode::$admin_enum['params_error'];
                $this->returnData['msg'] = '文件类型错误';
                return $this->return_result($this->returnData);
            }
            $temp_file = tempnam(sys_get_temp_dir(),"php");  //临时文件
            file_put_contents($temp_file,base64_decode($files['content']));        //文件流写入文件
            //创建文件夹
            $time = date('Ymd',time());
            $tmpName = time() . rand(10000, 99999);
            $newFile = $tmpName . '.' . strtolower(str_replace("image/", "", $files['type']));
            $cloud_file = '/uploads/picture/'.$time;
            $img_name = $cloud_file.'/'.$newFile;
            $pictureModel = new Picture();
            $url = $pictureModel->uploadImg($img_name,$temp_file);
            if (!$url){
                $this->returnData = ErrorCode::$admin_enum['params_error'];
                $this->returnData['msg'] = '上传图片失败';
                return $this->return_result($this->returnData);
            }
            $url['ObjectURL'] = str_ireplace($host,'',$url['ObjectURL']);
            $picture_info['name'] = $img_name;
            $picture_info['uid'] = 0;
            $picture_info['type'] = $files['type'];
            $picture_info['status'] = 1;
            $picture_info['src'] = $url['ObjectURL'];
            $picture_info['time'] = Carbon::now();
            DB::table('picture')->insertGetId($picture_info);
            $result['code'] = 0;
            $result['msg'] = '请求成功';
            $result['data']['title'] = $img_name;
            $result['data']['url'] = $url['ObjectURL'];
            $result['data']['type'] = $files['type'];
            $result['data']['id'] = $url['ObjectURL'];
        } else{
            $base64_img = $request->file('file');
            $res = (new UploadFile([
                'upload_dir' => './uploads/picture/',
                'type'       => ['image/jpg', 'image/png', 'image/jpeg', 'image/bmp']
            ]))->upload($base64_img);
            if($res['code'] > 0) {
                return $this->return_result($res);
            }
            $picture_info['name'] = $res['name'];
            $picture_info['uid'] = 0;
            $picture_info['type'] = $res['type'];
            $picture_info['status'] = 1;
            $picture_info['src'] = $res['url'];
            $picture_info['time'] = Carbon::now();
            DB::table('picture')->insertGetId($picture_info);
            $result['code'] = 0;
            $result['msg'] = '请求成功';
            $result['data']['title'] = $res['name'];
            $result['data']['url'] = UpTools::processingPictures($res['url']);
            $result['data']['type'] = $res['type'];
            $result['data']['id'] = $res['url'];
        }
        return $this->return_result($result);
    }
}
